//
//  WWComposeView.swift
//  SinWEiBo
//
//  Created by 胡良志 on 2018/4/17.
//  Copyright © 2018年 HuLiangZhi. All rights reserved.
//

import UIKit
import pop


class WWComposeView: UIView {

    //Button数组
    lazy var buttonArray : [WWComposeButton] = [WWComposeButton]()
    //截屏图片
    private lazy var backView:UIImageView = UIImageView(image: UIImage.snapShopCurrentWindow()) //截屏
    //生成模糊效果
    private lazy var toolbar : UIToolbar = UIToolbar(frame: UIScreen.main.bounds)
    
    //当外部调用这个方法,把当前视图加到外部视图上
    open func show (vc:UIViewController){
        vc.view.addSubview(self)

    }
    //当视图添加到父视图的时候会调用 -> 动画在这里面操作,不能再viewdidLoad方法中实现,显示会有问题
//    override func willMove(toSuperview newSuperview: UIView?) {
//        //开始动画
////        startAnimation()
//    }
    
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setupUI()
        //开始动画
        for (index,btn) in buttonArray.enumerated() {
            startAnimation(btn: btn, index: index)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI (){
        //创建一个toolbar覆盖到backView上形成模糊效果
//        let toolbar = UIToolbar(frame: UIScreen.main.bounds)
        toolbar.barStyle = .default
        addSubview(backView)
        addSubview(toolbar)
        addComposeButton()
    }

    //添加按钮
    private func addComposeButton(){
        let margin = (ScreenWidth - 3 * btnWidth) / 4 //间距
        
        for i in 0..<6 {
            let btn = WWComposeButton() //自定义Button
            let stand = i % 3 //列
            let row = i / 3 //行
//            print(i)
//            print("列--\(stand),行--\(row)")
   
            let x = margin + (btnWidth + margin) * CGFloat(stand)
            let y = margin + (btnHeight + margin) * CGFloat(row) + ScreenHeight
//            print("x--\(x),y--\(y)")
            btn.frame = CGRect(x: x, y: y, width: btnWidth, height: btnHeight)
            btn.tag = i
            let btnArr = ["tabbar_compose_friend","tabbar_compose_idea",
                          "tabbar_compose_more","tabbar_compose_music",
                          "tabbar_compose_photo","tabbar_compose_review"]
            let image = UIImage(named: btnArr[i])
            btn.setImage(image, for: .normal)
            btn.setTitle("发布微博\(i)", for: .normal)
            btn.setTitleColor(UIColor.black, for: .normal)
            buttonArray.append(btn)
            addSubview(btn)
            btn.addTarget(self, action:#selector(composeDidClick(btn:)), for: .touchUpInside)
        }
    }
    //点击事件
    @objc private func composeDidClick (btn:WWComposeButton){
        for compseBtn in self.buttonArray {
            UIView .animate(withDuration: 0.5, animations: {
                if compseBtn == btn{
                    compseBtn.transform = CGAffineTransform(scaleX: 2, y: 2)
                }else{
                    compseBtn.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
                    compseBtn.alpha = 0.1
                }
            }, completion: { (_) in
                let className = "SinWEiBo.WWComposeController"
                guard let classType = NSClassFromString(className) as? UIViewController.Type else {
                    
                    return}
                
                let vc = classType.init()
                
                let nav = UINavigationController(rootViewController: vc)
                //在view中无法跳转控制器,因此切换window的跟控制器
                UIApplication.shared.keyWindow?.rootViewController?.present(nav, animated: true, completion: {
                    //跳转完成之后移除当前视图,
                    self.removeFromSuperview()
                })
            })
        }
        print(btn.tag)
    }
    
    //开始动画
    private func startAnimation (btn:WWComposeButton,index:Int,isShow:Bool = true){
        /*
         POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
         CGPoint point = [tap locationInView:self.view];
         springAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, point.x, point.y)];
         //弹性值
         springAnimation.springBounciness = 20.0;
         //弹性速度
         springAnimation.springSpeed = 20.0;
         [_springView pop_addAnimation:springAnimation forKey:@"changeframe"];
         */
//        for (index,btn) in buttonArray.enumerated() {
            let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            springAnimation?.springSpeed = 10//弹性速度
            springAnimation?.springBounciness = 10//弹性值
            //每个按钮延迟
            springAnimation?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
            let centerY = btn.center.y + CGFloat( isShow ? -420 : 420)
            springAnimation?.toValue = CGPoint(x:btn.center.x, y: centerY)
            btn.pop_add(springAnimation, forKey: nil)
//        }
        
    }
    
    //退出
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //结束动画
        for (index,btn) in buttonArray.reversed().enumerated() {
           startAnimation(btn: btn, index: index, isShow: false)
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            self.removeFromSuperview()
        }
    }


}

